<?php

namespace App\Http\Controllers\Divide;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Divide\Category;
use App\Models\Divide\City;
use App\Models\Divide\PdLevel;
use App\Models\Divide\SplitSystem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CategoryController extends Controller
{
    use ResponseData;
    public function list(Category $model, Request $request){
        $data = $model->query()->get();
        return $this->success($data);
    }
    public function getPlist(Category $model, Request $request){
        $data = $model->query()
            ->select('category_id','category_name')
            ->get();
        return $this->success($data);
    }
    // 获取导航数据
    public function getNav(Category $model, Request $request){
        $data = $model->select('category_id','category_name','category_types')->where('status',1)->get();
        return $this->success($data);
    }
    protected function getLastId(){
        $data = Category::query()
            ->select('category_id')
            ->where('category_id','>',0)
            ->orderByDesc('category_id')
            ->first();
        if($data){
            return $data->category_id-1000;
        }
        return -1;
    }
    protected function setCitys($category_id){
        $citys = City::query()
            ->select('city_name','pid','area_code','level')
            ->where('city_id','>',0)
            ->get();
        foreach ($citys as &$city){
            $city->category_id = $category_id;
        }
        unset($city);
        DB::table('city_split_ratios')->insert($citys->toArray());
    }
    public function save(Category $model, Request $request){
        $data = $request->only('category_name','category_id','category_types');
        if(!$request->filled('category_id')) {
//            $data['mode'] = $this->getLastId();
            $res = $model->create($data);
            if($res){
                $this->setCitys($res->category_id);
            }
            if($res&&$res->category_id){
                if($request->filled('category_types')){
                    $arr = is_array($request->category_types)?$request->category_types:json_decode($request->category_types,true);
                    foreach ($arr as $item){
                        $this->createSystem($res,$item);
                        if($item['id']==4){
                            $this->setConfig($res->category_id);
                        }
                    }
                }
            }
        }else{
            $divide = $model->query()->where('category_id',$request->system_id)
                ->first();
            if($divide){
                $divide->update($data);
            }
        }
        return $this->success('更新成功');
    }
    public function testSetConfig(Request $request){
        $this->setConfig($request->category_id);
        return $this->success('请求数据'.$request->category_id);
    }
    protected function setConfig($category_id){
        $configs = PdLevel::query()->where('mode',-1)
            ->get();
        foreach ($configs as $key=>$config){
            $data[$key]['mode'] = $category_id;
            $data[$key]['level'] = $config->level;
            $data[$key]['name'] = $config->name;
            $data[$key]['threshold'] = $config->threshold;
            $data[$key]['first_ratio'] = $config->first_ratio;
            $data[$key]['second_ratio'] = $config->second_ratio;
            $data[$key]['v2_ratio'] = $config->v2_ratio;
            $data[$key]['award'] = $config->award;
            $data[$key]['is_borker'] = $config->is_borker;
            $data[$key]['is_sale'] = $config->is_sale;
            $data[$key]['first_ratio1'] = $config->first_ratio1;
            $data[$key]['second_ratio1'] = $config->second_ratio1;
            $data[$key]['first_ratio3'] = $config->first_ratio3;
            $data[$key]['is_inte'] = $config->is_inte;
            $data[$key]['inte_scale'] = $config->inte_scale;
            $data[$key]['is_inte_buy'] = $config->is_inte_buy;
            $data[$key]['inte_scale_buy'] = $config->inte_scale_buy;
        }
        DB::table('pd_level')->insert($data);
    }
    // 合伙制
    protected function createSystem($data,$type){
        $data = [
            'category_id'=>$data->category_id,
            'system_name'=>$type['name'],
            'type'=>$type['id'],
            'ratio'=>$type['ratio']
        ];
        SplitSystem::query()->create($data);
    }
}
